package com.mylove.textprogressbar;

import android.os.Handler;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author myLove
 * @time 2017/12/29 17:18
 * @e-mail love@yanyi.red
 * @overview
 */

public class RefreshActivity extends BaseActivity {
    private RefreshLayout mRefresh;
    private EditText edit;
    private Button but;
    private Button but2;

    @Override
    protected int setContentView() {
        return R.layout.act_refresh;
    }

    @Override
    protected void init() {
        mRefresh = $(R.id.refresh);
        edit = $(R.id.size);
        but = $(R.id.but);
        but2 = $(R.id.but2);

    }

    @Override
    protected void onResume() {
        super.onResume();
        edit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mRefresh.setRefresh(true);
        //自动刷新回调
        mRefresh.setOnAutoRefreshListener(new RefreshLayout.OnAutoRefreshListener() {
            @Override
            public void onAutoRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRefresh.setRefresh(false);
                    }
                }, 2000);
            }
        });
        //手动刷新回调
        mRefresh.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        //关闭刷新
                        mRefresh.setRefresh(false);
                    }
                });
            }
        });
        boolean b = mRefresh.isIndex();
        if (b) {
            but.setVisibility(View.VISIBLE);
        } else {
            but.setVisibility(View.GONE);
        }
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = edit.getText().toString();
                float size = Float.valueOf(TextUtils.isEmpty(str) ? "50" : str);
                mRefresh.setSize(size);
            }
        });
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefresh.setIndex(!mRefresh.isIndex());
                boolean b = mRefresh.isIndex();
                if (b) {
                    but.setVisibility(View.VISIBLE);
                } else {
                    but.setVisibility(View.GONE);
                }
                mRefresh.setRefresh(true);
            }
        });
    }
}
